package com.acme.bankapp.domain.bank;

public class CheckingAccount extends AbstractAccount {

    private double overdraft = 0;


    public CheckingAccount(double amount) {
        super(amount);
    }

    public double getOverdraft() {
        return overdraft;
    }

    public CheckingAccount(double amount, double overdraft) {
        super(amount);
        if (overdraft < 0){
            throw new IllegalArgumentException("Negative overdraft");
        }

        this.overdraft = overdraft;
    }

    @Override
    public String getAccountType() {
        return "c";
    }

    @Override
    public void withdraw(double amount) throws OverDraftLimitExceededException {


        if (overdraft + balance < amount) {
            throw new OverDraftLimitExceededException(this);//TODO ADD CLIENT!!!!
        }

        if (amount <= balance) {
            balance -= amount;
            return;
        }


        overdraft -= amount - balance;
        balance = 0;

    }

    @Override
    public double maximumAmountToWithdraw() {
        return balance + overdraft;
    }

    @Override
    public boolean equals(Object o) {
        if (!super.equals(o)) return false;
        CheckingAccount temp = (CheckingAccount) o;

        if (temp.overdraft != this.overdraft) return false;


        return true;
    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        long temp;
        temp = Double.doubleToLongBits(overdraft);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        return result;
    }

    @Override
    public String toString() {
        return super.toString() +
                "overdraft=" + overdraft +
                '}';
    }
}
